from datetime import date
from tortoise import fields
from models.base_model import TimestampMixin
from uuid import uuid4

class Permission(TimestampMixin):
    permission_name = fields.CharField(unique=True, max_length=32, description="权限名称")
    permission_desc = fields.CharField(null=True, max_length=255, description='权限描述')

    class Meta:
        table_description = "权限表"
        table = "permission"

class Role(TimestampMixin):
    role_name = fields.CharField(unique=True, max_length=32, description="角色名称")
    role_status = fields.BooleanField(default=False, description="True:启用 False:禁用")
    role_desc = fields.CharField(null=True, max_length=255, description='角色描述')
    access: fields.ManyToManyRelation[Permission] = \
        fields.ManyToManyField("base.Permission", related_name="role", on_delete=fields.CASCADE)

    class Meta:
        table_description = "角色表"
        table = "role"

class Family(TimestampMixin):
    family_id = fields.UUIDField(default=uuid4, unique=True, description="家族唯一标识")
    password = fields.CharField(null=True, max_length=255)
    family_name = fields.CharField(max_length=50, description="家族名称")
    family_desc = fields.CharField(null=True, max_length=255, description="家族描述")
    family_status = fields.IntField(default=1, description="家族状态: 0-解散 1-正常")

    class Meta:
        table_description = "家族表"
        table = "family"

class User(TimestampMixin):
    username = fields.CharField(unique=True, max_length=64, description="用户名")
    password = fields.CharField(null=False, max_length=255)
    user_phone = fields.CharField(null=True, description="手机号", max_length=11)
    user_email = fields.CharField(null=True, description='邮箱', max_length=255)
    full_name = fields.CharField(null=True, max_length=16, description='姓名')
    birthday = fields.DateField(default=date(1970, 1, 1), description='生日')
    sex = fields.IntField(default=0, description='0未知 1男 2女')
    user_status = fields.IntField(default=0, description='0未激活 1正常 2禁用')
    header_img = fields.CharField(null=True, max_length=255, description='头像')
    remarks = fields.CharField(null=True, max_length=30, description="备注")
    role: fields.ManyToManyRelation[Role] = \
        fields.ManyToManyField("base.Role", related_name="user", on_delete=fields.CASCADE)
    family: fields.ForeignKeyRelation[Family] = \
        fields.ForeignKeyField("base.Family", related_name="members", null=True, on_delete=fields.SET_NULL)

    class Meta:
        table_description = "用户表"
        table = "user"

class UserWechat(TimestampMixin):
    city = fields.CharField(null=True, max_length=255, description='城市')
    country = fields.CharField(null=True, max_length=255, description='国家')
    headimgurl = fields.CharField(null=True, max_length=255, description='微信头像')
    nickname = fields.CharField(null=True, max_length=255, description='微信昵称')
    openid = fields.CharField(unique=True, max_length=255, description='openid')
    unionid = fields.CharField(unique=True, null=True, max_length=255, description='unionid')
    province = fields.CharField(null=True, max_length=255, description='省份')
    sex = fields.IntField(null=True, description='性别')
    user: fields.OneToOneRelation[User] = \
        fields.OneToOneField("base.User", related_name="wechat", on_delete=fields.CASCADE)

    class Meta:
        table_description = "用户微信"
        table = "user_wechat"

class UserRelation(TimestampMixin):
    parent: fields.ForeignKeyRelation[User] = \
        fields.ForeignKeyField("base.User", related_name="children_relations", on_delete=fields.CASCADE)
    child: fields.ForeignKeyRelation[User] = \
        fields.ForeignKeyField("base.User", related_name="parent_relations", on_delete=fields.CASCADE)
    relation_type = fields.IntField(description="关系类型: 1-父子关系 2-母子关系 3-夫妻关系")
    remarks = fields.CharField(null=True, max_length=255, description="关系备注")
    family: fields.ForeignKeyRelation[Family] = \
        fields.ForeignKeyField("base.Family", related_name="relations", null=True, on_delete=fields.SET_NULL)

    class Meta:
        table_description = "用户关系表"
        table = "user_relation"
        indexes = (("child", "relation_type"), ("parent", "relation_type"))